package src.main.com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import src.main.com.example.springboot.common.Result;
import src.main.com.example.springboot.entity.User;
import src.main.com.example.springboot.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/user")
public class UserController
{


    @Resource
    UserMapper userMapper;


    @PostMapping("/login")
    public Result<?> login(@RequestBody User user)
    {
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        //判断查询是否存在
        if (res == null)
        {
            System.out.println("存在用户");
            return Result.error("-1", "用户名或密码错误");
        }
        return Result.success(res);
    }


    @PostMapping("/register")
    public Result<?> register(@RequestBody User user)
    {

        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));

        if (res != null)
        {
            return Result.error("-1", "用户名已存在!");
        }

        if (user.getPassword() == null)
        {
            user.setRole(2);
            user.setPassword("123456");
        }
        user.setRole(2);
        userMapper.insert(user);
        return Result.success();
    }


    @PostMapping

    public Result<?> save(@RequestBody User user)
    {

        if (user.getPassword() == null)
        {
            user.setRole(2);
            user.setPassword("123456");
        }
        user.setRole(2);
        userMapper.insert(user);
        return Result.success();
    }


    @PutMapping
    public Result<?> update(@RequestBody User user)
    {
        userMapper.updateById(user);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id)
    {
        userMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable long id)
    {
        return Result.success(userMapper.selectById(id));
    }

    @GetMapping("/all")
    public Result<?> finAll(@PathVariable long id)
    {
        return Result.success(userMapper.selectList(null));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search)
    {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        if (StrUtil.isNotBlank(search))
        {
            wrapper.like(User::getNickName, search);
        }
        Page<User> userPage = userMapper.findPage(new Page<>(pageNum, pageSize));
        return Result.success(userPage);
    }

}
